<!-- This HTML file has been created by texi2html 1.30
     from ../r4rs.texi on 14 July 1994 -->

<TITLE>Scheme - Program structure</TITLE>
<P>Go to the <A HREF="r4rs_6.htm">previous</A>, <A HREF="r4rs_8.htm">next</A> section.<P>
<H1><A NAME="SEC40" HREF="r4rs_toc.htm#SEC40">Program structure</A></H1>
<P>
<H2><A NAME="SEC41" HREF="r4rs_toc.htm#SEC41">Programs</A></H2>
<P>
A Scheme program consists of a sequence of expressions and definitions.
Expressions are described in section <A HREF="r4rs_6.htm#SEC25">Expressions</A>;
definitions are the subject of the rest of the present chapter.
<P>
Programs are typically stored in files or entered interactively to a
running Scheme system, although other paradigms are possible;
questions of user interface lie outside the scope of this report.
(Indeed, Scheme would still be useful as a notation for expressing
computational methods even in the absence of a mechanical
implementation.)
<P>
Definitions occurring at the top level of a program can be interpreted
declaratively.  They cause bindings to be created in the top level
environment.  Expressions occurring at the top level of a program are
interpreted imperatively; they are executed in order when the program is
invoked or loaded, and typically perform some kind of initialization.
<P>
<H2><A NAME="SEC42" HREF="r4rs_toc.htm#SEC42">Definitions</A></H2>
<P>
Definitions are valid in some, but not all, contexts where expressions
are allowed.  They are valid only at the top level of a &#60;program&#62;
and, in some implementations, at the beginning of a &#60;body&#62;.
<A NAME="IDX148"></A>
<P>
A definition should have one of the following forms:
<A NAME="IDX149"></A>
<P>
<UL>
<P>
<LI><CODE>(define &#60;variable&#62; &#60;expression&#62;)</CODE>
<P>
This syntax is essential.
<P>
<LI><CODE>(define (&#60;variable&#62; &#60;formals&#62;) &#60;body&#62;)</CODE>
<P>
This syntax is not essential.  &#60;Formals&#62; should be either a
sequence of zero or more variables, or a sequence of one or more
variables followed by a space-delimited period and another variable (as
in a lambda expression).  This form is equivalent to
<PRE>
(define &#60;variable&#62;
  (lambda (&#60;formals&#62;) &#60;body&#62;)).
</PRE>
<P>
<LI><CODE>(define (&#60;variable&#62; . &#60;formal&#62;) &#60;body&#62;)</CODE>
<P>
This syntax is not essential.  &#60;Formal&#62; should be a single
variable.  This form is equivalent to
<PRE>
(define &#60;variable&#62;
  (lambda &#60;formal&#62; &#60;body&#62;)).
</PRE>
<P>
<LI><CODE>(begin &#60;definition 1&#62; ...)</CODE>
<P>
This syntax is essential.  This form is equivalent to the set of
definitions that form the body of the <CODE>begin</CODE>.
<A NAME="IDX150"></A>
<P>
</UL>
<P>
<H3><A NAME="SEC43" HREF="r4rs_toc.htm#SEC43">Top level definitions</A></H3>
<P>
At the top level of a program, a definition
<PRE>
(define &#60;variable&#62; &#60;expression&#62;)
</PRE>
has essentially the same effect as the assignment expression
<PRE>
(set! &#60;variable&#62; &#60;expression&#62;)
<A NAME="IDX151"></A></PRE>
if &#60;variable&#62; is bound.  If &#60;variable&#62; is not bound,
however, then the definition will bind &#60;variable&#62; to a new
location before performing the assignment, whereas it would be an error
to perform a <CODE>set!</CODE> on an unbound
<A NAME="IDX153"></A>
<A NAME="IDX152"></A>
variable.
<P>
<PRE>
(define add3
  (lambda (x) (+ x 3)))
(add3 3)                    =>  6
(define first car)
(first '(1 2))              =>  1
</PRE>
<P>
All Scheme implementations must support top level definitions.
<P>
Some implementations of Scheme use an initial environment in
which all possible variables are bound to locations, most of
which contain undefined values.  Top level definitions in
such an implementation are truly equivalent to assignments.
<P>
<H3><A NAME="SEC44" HREF="r4rs_toc.htm#SEC44">Internal definitions</A></H3>
<P>
Some implementations of Scheme permit definitions to occur at the
beginning of a &#60;body&#62; (that is, the body of a <CODE>lambda</CODE>,
<CODE>let</CODE>, <CODE>let*</CODE>, <CODE>letrec</CODE>, or <CODE>define</CODE>
<A NAME="IDX155"></A>
<A NAME="IDX156"></A>
<A NAME="IDX157"></A>
<A NAME="IDX154"></A>
expression).  Such
<A NAME="IDX158"></A>
definitions are known as <EM>internal definitions</EM>
<A NAME="IDX159"></A>
as opposed to the top level definitions described above.
The variable defined by an internal definition is local to the
&#60;body&#62;.  That is, &#60;variable&#62; is bound rather than assigned,
and the region of the binding is the entire &#60;body&#62;.  For example,
<P>
<PRE>
(let ((x 5))
  (define foo (lambda (y) (bar x y)))
  (define bar (lambda (a b) (+ (* a b) a)))
  (foo (+ x 3)))            =>  45
</PRE>
<P>
A &#60;body&#62; containing internal definitions can always be converted
into a completely equivalent <CODE>letrec</CODE> expression.  For example, the
<A NAME="IDX160"></A>
<CODE>let</CODE> expression in the above example is equivalent to
<A NAME="IDX161"></A>
<P>
<PRE>
(let ((x 5))
  (letrec ((foo (lambda (y) (bar x y)))
           (bar (lambda (a b) (+ (* a b) a))))
    (foo (+ x 3))))
</PRE>
<P>
Just as for the equivalent <CODE>letrec</CODE> expression, it must be
<A NAME="IDX162"></A>
possible to evaluate each &#60;expression&#62; of every internal
definition in a &#60;body&#62; without assigning or referring to
the value of any &#60;variable&#62; being defined.
<P>
<P>Go to the <A HREF="r4rs_6.htm">previous</A>, <A HREF="r4rs_8.htm">next</A> section.<P>
&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>&nbsp;<p>